package flst.ipiweb.struts.action.administrateur;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.proxy.ServiceAdministrateurProxy;
import flst.ipiweb.transverse.Administrateur;
import flst.ipiweb.transverse.IPIException;

public class DeleteAdminAction extends Action{
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response){
		
			//On récupère la session
			HttpSession session = request.getSession();
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
			
			Administrateur admin = new Administrateur();
			
			int idUser = -1;
			
			try{
				idUser = Integer.parseInt(request.getParameter("id"));
			}catch(Exception e){
				return mapping.findForward("list_admin");
			}
			
			admin.setIdUser(idUser);
			
			//Instanciation du service administrateur
			ServiceAdministrateurProxy sAdministrateur = new ServiceAdministrateurProxy((Boolean) session.getAttribute("isSuper"));
			
			try {
				
				sAdministrateur.deleteAdministrateur(admin);
				session.setAttribute("success","AdminSuccess.delete");
				session.setAttribute("error", null);
				
			} catch (IPIException e) {
				
				session.setAttribute("success",null);
				session.setAttribute("error", e.getMessage());
				
			}
			
			return mapping.findForward("list_admin");
		}

}
